﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

namespace Game1
{
    class Program
    {

        public static bool IsNodeInsideField (int x, int y, GameField gf)
        {
            if ((y > gf.GameFieldY - 1) && (y < (gf.GameFieldY + gf.Heigth)) &&
                (x > gf.GameFieldX - 1) && x < (gf.GameFieldX + gf.Width))
               return true;
            return false;
        }

        public static bool IsNodeCanBeMoved(List<GameNode> listOfNodes, int x, int y)
        {
            bool isMovable = true;
            foreach (GameNode node in listOfNodes)
            {
                if ((x == node.X && y == node.Y)) //&& (node != this))
                {
                    return false;
                }
            }
            return isMovable;
        }

        static void Main(string[] args)
        {
            Console.CursorVisible = false;
            GameField newGF = new GameField(5, 5, 10, 10);
            Drawer.DrawGameField(newGF, '*');
            Console.WriteLine();

            Drawer newPrinter = new Drawer();

            Random rndXYCoordinate = new Random();
            List<GameNode> listOfGameNodes = new List<GameNode>();

            for (int i = 0; i < 3; i++)
            {
                //Add checkk
                int xStart = rndXYCoordinate.Next(newGF.GameFieldX, (newGF.Width + newGF.GameFieldX) - 1);
                int yStart = rndXYCoordinate.Next(newGF.GameFieldY, (newGF.GameFieldY + newGF.Heigth) - 1);
                GameNode newNode = new GameNode(xStart, yStart);
                listOfGameNodes.Add(newNode);
                newPrinter.ClearGameNode(newNode);
                newPrinter.DrawGameNode(newNode);
            }

            //listOfGameNodes[0].color = ConsoleColor.Blue;
            //listOfGameNodes[1].color = ConsoleColor.Cyan;
            //listOfGameNodes[2].color = ConsoleColor.Red;

            GameNode exit = new GameNode(0, 0);
            exit.color = ConsoleColor.Red;

            do
            {
                exit.X = rndXYCoordinate.Next(newGF.GameFieldX, (newGF.Width + newGF.GameFieldX) - 1);
                exit.Y = rndXYCoordinate.Next(newGF.GameFieldY, (newGF.GameFieldY + newGF.Heigth) - 1); 
            }
            while (IsNodeCanBeMoved(listOfGameNodes, exit.X, exit.Y) && IsNodeInsideField(exit.X,exit.Y, newGF));
            newPrinter.DrawGameNode(exit);



            Random rndOper = new Random();
            List<GameNode> listOfWinners = new List<GameNode>();

            do
            {
                foreach (GameNode node in listOfGameNodes)
                {
                    int caseSwitchOperation = rndOper.Next(1, 5);
                    switch (caseSwitchOperation)
                    {
                        case 1:
                            if (IsNodeCanBeMoved(listOfGameNodes, node.X, node.Y + 1)
                                && IsNodeInsideField(node.X, node.Y + 1, newGF))
                            {
                                newPrinter.ClearGameNode(node);
                                node.MoveDown();
                                newPrinter.DrawGameNode(node);
                            }
                            break;
                        case 2:
                            if (IsNodeCanBeMoved(listOfGameNodes, node.X, node.Y - 1)
                                && IsNodeInsideField(node.X, node.Y - 1, newGF))
                            {
                                newPrinter.ClearGameNode(node);
                                node.MoveUp();
                                newPrinter.DrawGameNode(node);
                            }
                            break;
                        case 3:
                            if (IsNodeCanBeMoved(listOfGameNodes, node.X + 1, node.Y)
                                && IsNodeInsideField(node.X + 1, node.Y, newGF))
                            {
                                newPrinter.ClearGameNode(node);
                                node.MoveRight();
                                newPrinter.DrawGameNode(node);
                            }
                            break;
                        case 4:
                            if (IsNodeCanBeMoved(listOfGameNodes, node.X - 1, node.Y)
                                && IsNodeInsideField(node.X - 1, node.Y, newGF))
                            {
                                newPrinter.ClearGameNode(node);
                                node.MoveLeft();
                                newPrinter.DrawGameNode(node);
                            }
                            break;
                    }

                    if (node.X == exit.X && node.Y == exit.Y)
                    {
                        listOfWinners.Add(node);
                        listOfGameNodes.Remove(node);
                    }

                }


            }
            while (listOfGameNodes.Count > 1);
            Console.ReadKey();
        }

    }

}
